import folium
%matplotlib inline
m = folium.Map(location=[45.5236, -122.6750])
m
m = folium.Map(
location=[45.372, -121.6972],
zoom_start=12,
tiles='Stamen Terrain'
)
tooltip = 'Click me!'
folium.Marker([45.3288, -121.6625], popup='<i>Mt. Hood Meadows</i>').add_to(m)
folium.Marker([45.3311, -121.7113], popup='<b>Timberline Lodge</b>').add_to(m)
m
map_5 = folium.Map(location=[45.5236, -122.6750],
zoom_start=13)
folium.RegularPolygonMarker(
[45.5012, -122.6655],
popup='Ross Island Bridge',
fill_color='#132b5e',
number_of_sides=3,
radius=10
).add_to(map_5)
folium.RegularPolygonMarker(
[45.5132, -122.6708],
popup='Hawthorne Bridge',
fill_color='#45647d',
number_of_sides=4,
radius=10
).add_to(map_5)
folium.RegularPolygonMarker(
[45.5275, -122.6692],
popup='Steel Bridge',
fill_color='#769d96',
number_of_sides=6,
radius=10
).add_to(map_5)
folium.RegularPolygonMarker(
[45.5318, -122.6745],
popup='Broadway Bridge',
fill_color='#769d96',
number_of_sides=8,
radius=10
).add_to(map_5)
map_5
import osmnx as ox, networkx as nx
import warnings
import requests
import contextlib
try:
from functools import partialmethod
except ImportError:
# Python 2 fallback: https://gist.github.com/carymrobbins/8940382
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance, *(self.args or ()), **(self.keywords or {}))
@contextlib.contextmanager
def no_ssl_verification():
old_request = requests.Session.request
requests.Session.request = partialmethod(old_request, verify=False)
warnings.filterwarnings('ignore', 'Unverified HTTPS request')
yield
warnings.resetwarnings()
requests.Session.request = old_request
# download the street network for Piedmont, CA
with no_ssl_verification():
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
# ox.graph_from_place('Piedmont, California, USA', network_type='drive')
graph_map = ox.plot_graph_folium(G, popup_attribute='name', edge_width=2)
graph_map
# use networkx to calculate the shortest path between two nodes
origin_node = list(G.nodes())[0]
destination_node = list(G.nodes())[-1]
route = nx.shortest_path(G, origin_node, destination_node)
# plot the route with folium
route_map = ox.plot_route_folium(G, route)
route_map
# plot the route with folium on top of the previously created graph_map
route_graph_map = ox.plot_route_folium(G, route, popup_attribute='length')
route_graph_map
# plot the route with folium on top of the previously created graph_map
route_graph_map = ox.plot_route_folium(G, route, route_map=graph_map, popup_attribute='length')
route_graph_map
m = folium.Map(location=[40.760094, -73.994618])
folium.Marker([40.760094, -73.994618]).add_to(m)
folium.Marker([40.788390, -73.974700]).add_to(m)
m
points = [[40.760094, -73.994618], [40.788390, -73.974700]]
ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)
for each in points:
folium.Marker(each).add_to(my_map)
folium.PolyLine(points, color='red', weight=2.5, opacity=1).add_to(my_map)
my_map
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
# load CitiBike data
bike_data = pd.read_csv('data/201706-citibike-tripdata.csv')
bike_data.shape
bike_data.columns = bike_data.columns.str.replace('\'','')
bike_data.columns
bike_data['route_id'] = bike_data['start station id'].astype(str) + '_' + bike_data['end station id'].astype(str)
sns.distplot(bike_data[bike_data.route_id == '2006_2006'].tripduration)
top_routes = bike_data.where(bike_data['start station id'] != bike_data['end station id'])['route_id'].value_counts()[:20]
top_routes
top_routes.index
points = []
for route in top_routes.index:
point = [[bike_data.loc[bike_data.route_id == route]['start station latitude'].iloc[0], bike_data.loc[bike_data.route_id == route]['start station longitude'].iloc[0]],
[bike_data.loc[bike_data.route_id == route]['end station latitude'].iloc[0], bike_data.loc[bike_data.route_id == route]['end station longitude'].iloc[0]]
]
points.append(point)
print(points)
ave_lat = sum(p[0][0] for p in points)/len(points)
ave_lon = sum(p[0][1] for p in points)/len(points)
my_map = folium.Map(location=[ave_lat, ave_lon], tiles='Cartodb Positron', zoom_start=12)
for each in points:
folium.Marker(each[0]).add_to(my_map)
folium.Marker(each[1]).add_to(my_map)
folium.PolyLine(each, color='red', weight=2.0, opacity=1).add_to(my_map)
# folium.PolyLine(points, color='red', weight=2.5, opacity=1).add_to(my_map)
my_map
points[0]
start_rides = []
for route in top_routes.index:
point = [bike_data.loc[bike_data.route_id == route]['start station latitude'].iloc[0], bike_data.loc[bike_data.route_id == route]['start station longitude'].iloc[0]]
start_rides.append(point)
print(start_rides)
import folium.plugins as plugins
m = folium.Map(location=[ave_lat, ave_lon], zoom_start=12)
hm = plugins.HeatMap(
start_rides,
# index=time_index,
# auto_play=True,
# max_opacity=0.3
)
hm.add_to(m)
m
bike_data.head()
time_index = bike_data[:100].starttime
points = []
for t in range(len(time_index)):
point = [[bike_data.iloc[t]['start station latitude'], bike_data.iloc[t]['start station longitude']]]
points.append(point)
m = folium.Map(location=[ave_lat, ave_lon], zoom_start=12)
hm = plugins.HeatMapWithTime(points,
# index=time_index,
auto_play=True,
max_opacity=0.3
)
hm.add_to(m)
m
from datetime import datetime, timedelta
np.random.seed(3141592)
initial_data = (
np.random.normal(size=(100, 2)) * np.array([[.01, .01]]) +
np.array([[ave_lat, ave_lon]])
)
move_data = np.random.normal(size=(100, 2)) * 0.00001
data = [(initial_data + move_data * i).tolist() for i in range(100)]
time_index = [
(datetime.now() + k * timedelta(1)).strftime('%Y-%m-%d') for
k in range(len(data))
]
m = folium.Map([ave_lat, ave_lon], tiles='Cartodb Positron', zoom_start=13)
hm = plugins.HeatMapWithTime(
data,
index=time_index,
auto_play=True,
max_opacity=0.3
)
hm.add_to(m)
m
len(data)
len(points)
points[:5]
m = folium.Map([ave_lat, ave_lon], tiles='Cartodb Positron', zoom_start=13)
hm = plugins.HeatMapWithTime(
data,
# index=time_index,
auto_play=True,
max_opacity=0.3
)
hm.add_to(m)
m
time_index = bike_data[:1000].starttime
points = []
for t in range(len(time_index)):
point = [[bike_data.iloc[t]['start station latitude'], bike_data.iloc[t]['start station longitude']]]
points.append(point)
from itertools import chain
m = folium.Map([ave_lat, ave_lon], tiles='Cartodb Positron', zoom_start=12)
hm = plugins.HeatMap(list(chain(*points)), radius=10)
hm.add_to(m)
m